﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game_Engine
{
    public class Renderer : Component
    {
        Model model;
        Material material;

        public Model Model
        {
            get { return model; }
            set { model = value; }
        }

        public virtual Material Material
        {
            get { return material; }
            set {material = value;}
        }

        public Renderer()
            : base()
        { }

        public Renderer(Model m) : base()
        {
            Model = m;
        }

        public virtual void Draw()
        { }

        public void Draw(Camera camera, GraphicsDevice GraphicsDevice)
        {
            if (Model != null)
            {
                if (Material != null && material.On)
                {
                    Material.Update(Host.Transform.World, camera, material.Light,
                        material.Color,  material.Sheen,
                        material.Offset, material.Tiling);
                    foreach (EffectPass pass in material.Effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                        foreach (ModelMesh mesh in model.Meshes)
                        {
                            foreach (ModelMeshPart part in mesh.MeshParts)
                            {
                                GraphicsDevice.SetVertexBuffer(part.VertexBuffer);
                                GraphicsDevice.Indices = part.IndexBuffer;
                                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.VertexOffset, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount);
                            }
                        }
                    }
                }
                else
                    model.Draw(Host.Transform.World, camera.View, camera.Projection);
            }
        }
    }
}
